package com.xinsoft.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.SysFileParam;
import com.xinsoft.entity.po.SysFileHis;
import com.xinsoft.mapper.SysFileHisMapper;
import com.xinsoft.service.SysFileHisService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author lijie
 * @description 针对表【sys_file_his(存储文件记录)】的数据库操作Service实现
 * @createDate 2023-05-19 11:11:02
 */
@Service
public class SysFileHisServiceImpl extends ServiceImpl<SysFileHisMapper, SysFileHis>
        implements SysFileHisService {

    @Resource
    SysFileHisMapper fileHisMapper;

    //基本路
    @Value("${file.upload.basePath}")
    private String basePath;
    //本地路径
    @Value("${file.upload.localPath}")
    private String localPath;

    @Override
    public Object findPageListByParam(SysFileParam param) {
        //分页
        Page<SysFileHis> page = new Page<SysFileHis>(param.getCurrentPage(), param.getPageSize());
        QueryWrapper<SysFileHis> queryWrapper = new QueryWrapper<>();
        //查询条件
        queryWrapper
                .like("file_type_id", param.getFileTypeId())
                .orderByDesc("create_time");
        IPage<SysFileHis> resultPage = fileHisMapper.selectPage(page, queryWrapper);
        //返回值
        return resultPage;
    }

    @Override
    public String uploadFilesByIO(MultipartFile file) {
        if (file.isEmpty()) {
            return null;
        }
        SimpleDateFormat ymd = new SimpleDateFormat("yyyyMMdd");
        String today = ymd.format(new Date());
        String path = localPath + File.separator + basePath + File.separator + today;

        String uuid = UUID.randomUUID().toString();
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        String newFileName = uuid + "." + suffix;
        String returnPath = basePath + File.separator + today + File.separator + newFileName;
        File filePath = new File(path, newFileName);
        if (!filePath.getParentFile().exists()) {
            filePath.getParentFile().mkdirs();
        }
        try {
            file.transferTo(filePath);
            // 生成文件上传记录
            // 开始写入标准历史
            SysFileHis fileHis = new SysFileHis();
            fileHis.setFileName(fileName);
            fileHis.setFileTypeId(Const.FileType.BASIC_FILE);
            fileHis.setFileUploadTime(DateUtil.formatDateTime(new Date()));
            fileHis.setFileDesc("上传文件：" + fileHis.getFileName() + ";生成后文件名：" + newFileName);
            fileHis.setFileUrl(Const.FileTypeProxyUrl.BASIC_FILE_URL + "/" + fileHis.getFileName());
            this.save(fileHis);
            return returnPath;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public SysFileHis uploadCusFile(MultipartFile file) {
        if (file.isEmpty()) {
            return null;
        }
        SimpleDateFormat ymd = new SimpleDateFormat("yyyyMMdd");
        String today = ymd.format(new Date());
        String path = localPath + File.separator + basePath + File.separator + today;

        String uuid = UUID.randomUUID().toString();
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        String newFileName = uuid + "." + suffix;
        String returnPath = basePath + File.separator + today + File.separator + newFileName;
        File filePath = new File(path, newFileName);
        if (!filePath.getParentFile().exists()) {
            filePath.getParentFile().mkdirs();
        }
        try {
            file.transferTo(filePath);
            // 生成文件上传记录
            // 开始写入标准历史
            SysFileHis fileHis = new SysFileHis();
            fileHis.setFileName(fileName);
            fileHis.setFileTypeId(Const.FileType.BASIC_FILE);
            fileHis.setFileUploadTime(DateUtil.formatDateTime(new Date()));
            fileHis.setFileDesc("上传文件：" + fileHis.getFileName() + ";生成后文件名：" + newFileName);
            fileHis.setFileUrl(Const.FileTypeProxyUrl.BASIC_FILE_URL + "/"  + today + "/" + newFileName);
            fileHis.setReturnPath(returnPath);
            this.save(fileHis);
            return fileHis;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}




